"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
import datetime

from csr_utils import log

from commons import testcase_constants, system_constatnts
from dao import suite_dao, project_dao, job_dao
from orm_domain.suite import Suite
from utils.bean_utils import BeanUtils


class SuiteService:
    # Todo： Service 参数 传递实体类。实体类相当于参数规范。 -> 李旭
    def save(self, suite: Suite):
        if BeanUtils.str_is_blank(suite.name):
            log.info("测试套件名称不能为空")
            return None
        if project_dao.get(id=suite.project_id,is_del = system_constatnts.NOT_DEL_FLAG) is None:
            log.info("测试套件绑定的project不存在")
            return None
        suite.name = suite.name.strip()
        if suite.serial_parallel is None:
            suite.serial_parallel = testcase_constants.SERIAL
        else:
            suite.serial_parallel = suite.serial_parallel
        suite_dao.save(suite)
        return suite



    def update(self, suite: Suite):
        '''
        更新套件的名称、串并行（默认串行）
        '''
        query_suite = suite_dao.get_by_suite_id(suite.id)
        if BeanUtils.str_is_blank(suite.name):
            log.info("测试套件名称不能为空")
            return None
        if query_suite is None:
            log.info("未找到对应测试套件")
            return None
        if project_dao.get(id=suite.project_id,is_del = system_constatnts.NOT_DEL_FLAG) is None:
            log.info("测试套件绑定的project不存在")
            return None
        if suite.serial_parallel is None:
            query_suite.serial_parallel = testcase_constants.SERIAL
        else:
            query_suite.serial_parallel = suite.serial_parallel
        query_suite.name = suite.name.strip()
        suite_dao.update()

    def delete(self, suite_id):
        suite = suite_dao.get_by_suite_id(suite_id)
        if suite == None:
            log.info("未找到对应的测试套件，无法删除")
            return None
        suite_dao.delete(suite)

    def get_by_id(self, suite_id):
        suite = suite_dao.get_by_suite_id(suite_id)
        return suite

    def list_by_job_id(self, job_id) -> [Suite]:
        job = job_dao.get_by_id(job_id)
        if job is None:
            log.info("未找到对应的job")
            return None
        return job.suites

    def bind_testcase_to_suite(self, suite_id, testcases: []):
        """
        关联测试用例
        """
        try:
            suite = suite_dao.get_by_suite_id(suite_id)
            if suite is None:
                log.info("套件不存在")
                return None
            suite.testcases = testcases
            suite_dao.update()
        except Exception as e:
            log.info("请检查测试用例列表数据是否正常", e)

    def unbind_testcase_to_suite(self, suite_id):
        """
        解除测试用例关联
        """
        try:
            suite = suite_dao.get_by_id(suite_id)
            if suite is None:
                log.info("套件不存在")
                return None
            suite.testcases = []
            suite_dao.update()
        except Exception as e:
            log.info("请检查测试用例列表数据是否正常", e)
